<template>
  <div class="layout-container">
    <!-- 顶部导航栏 -->
    <el-header class="layout-header">
      <div class="header-left">
        <el-button
          text
          @click="toggleSidebar"
          class="sidebar-toggle"
        >
          <el-icon size="20"><Expand v-if="isCollapse" /><Fold v-else /></el-icon>
        </el-button>
        <h1 class="system-title">考勤管理系统</h1>
      </div>
      
      <div class="header-right">
        <el-dropdown @command="handleCommand">
          <span class="user-info">
            <el-avatar :size="32" class="user-avatar">
              <el-icon><User /></el-icon>
            </el-avatar>
            <span class="username">{{ userStore.realName || userStore.username }}</span>
            <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">个人信息</el-dropdown-item>
              <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    
    <el-container class="layout-body">
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '280px'" class="layout-sidebar">
        <el-menu
          :default-active="$route.path"
          :collapse="isCollapse"
          :unique-opened="true"
          router
          class="sidebar-menu"
        >
          <template v-for="route in menuRoutes" :key="route.path">
            <!-- 单级菜单 -->
            <el-menu-item
              v-if="!route.children"
              :index="route.path"
              @click="handleMenuClick(route)"
            >
              <el-icon><component :is="route.meta?.icon || 'Document'" /></el-icon>
              <template #title>{{ route.meta?.title }}</template>
            </el-menu-item>
            
            <!-- 多级菜单 -->
            <el-sub-menu
              v-else
              :index="route.path"
            >
              <template #title>
                <el-icon><component :is="route.meta?.icon || 'Document'" /></el-icon>
                <span>{{ route.meta?.title }}</span>
              </template>
              <el-menu-item
                v-for="child in route.children"
                :key="child.path"
                :index="route.path + '/' + child.path"
                @click="handleMenuClick(child, route.path)"
              >
                {{ child.meta?.title }}
              </el-menu-item>
            </el-sub-menu>
          </template>
        </el-menu>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-main class="layout-main">
        <div class="main-content">
          <router-view />
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useUserStore } from '@/store/user'

const router = useRouter()
const userStore = useUserStore()

const isCollapse = ref(false)

// 获取菜单路由
const menuRoutes = computed(() => {
  const routes = router.getRoutes()
  const layoutRoute = routes.find(route => route.name === 'Layout')
  
  if (!layoutRoute || !layoutRoute.children) return []
  
  return layoutRoute.children.filter(route => {
    // 过滤掉dashboard和需要权限但用户没有权限的路由
    if (route.path === 'dashboard') return false
    
    // 检查权限
    if (route.meta?.requiresAdmin && !userStore.isAdmin) {
      return false
    }
    
    return route.meta?.title
  })
})

const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

const handleMenuClick = (route, parentPath = '') => {
  const fullPath = parentPath ? `/${parentPath}/${route.path}` : `/${route.path}`
  router.push(fullPath)
}

const handleCommand = async (command) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人信息功能待开发')
      break
    case 'logout':
      try {
        await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await userStore.logout()
      } catch (error) {
        // 用户取消操作
      }
      break
  }
}
</script>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.layout-header {
  background: linear-gradient(90deg, #4285f4 0%, #34a853 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  
  .header-left {
    display: flex;
    align-items: center;
    
    .sidebar-toggle {
      color: white;
      margin-right: 15px;
      
      &:hover {
        background: rgba(255, 255, 255, 0.1);
      }
    }
    
    .system-title {
      font-size: 20px;
      font-weight: 500;
      margin: 0;
    }
  }
  
  .header-right {
    .user-info {
      display: flex;
      align-items: center;
      cursor: pointer;
      padding: 5px 10px;
      border-radius: 4px;
      transition: background-color 0.3s;
      
      &:hover {
        background: rgba(255, 255, 255, 0.1);
      }
      
      .user-avatar {
        margin-right: 8px;
        background: rgba(255, 255, 255, 0.2);
      }
      
      .username {
        margin-right: 8px;
        font-size: 14px;
      }
      
      .dropdown-icon {
        font-size: 12px;
      }
    }
  }
}

.layout-body {
  flex: 1;
  overflow: hidden;
}

.layout-sidebar {
  background: #ffffff;
  border-right: 1px solid #e6e6e6;
  transition: width 0.3s;
  
  .sidebar-menu {
    border: none;
    height: 100%;
    
    :deep(.el-menu-item) {
      height: 48px;
      line-height: 48px;
      font-size: 14px;
      
      &.is-active {
        background: #e6f7ff;
        color: #1890ff;
        border-right: 3px solid #1890ff;
      }
      
      &:hover {
        background: #f5f5f5;
      }
    }
    
    :deep(.el-sub-menu) {
      .el-sub-menu__title {
        height: 48px;
        line-height: 48px;
        font-size: 14px;
        
        &:hover {
          background: #f5f5f5;
        }
      }
      
      .el-menu-item {
        padding-left: 60px !important;
        height: 45px;
        line-height: 45px;
        font-size: 14px;
      }
    }
  }
}

.layout-main {
  background: #f5f5f5;
  padding: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  
  .main-content {
    flex: 1;
    padding: 0;
    margin: 0;
    height: 100%;
    overflow: auto;
  }
}
</style>